package com.itboye.sunsun.product.center;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.itboye.sunsun.adapter.ProductDetailAdapter;
import com.itboye.sunsun.adapter.ShiPinAdapter;
import com.itboye.sunsun.beans.ProducenterDeleBean;
import com.itboye.sunsun.beans.ProducenterDeleBean.DeleteBeanProducer;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsunhome.R;

import java.util.ArrayList;
import java.util.List;

public class ProducenterShiPinFragment extends  BaseFragment {


	private XListView  xlistview;
	private int pageNum = 1;
	private int pageSize=10;
	private BaseAdapter adapter;
	 List<DeleteBeanProducer>dataList=new ArrayList<>();
	 
	 private RelativeLayout topbar;
	 String id;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_cahkan_gengduo_pinglun, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		id=getActivity().getIntent().getStringExtra("id");
		topbar.setVisibility(View.GONE);
		
		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(false);
		xlistview.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pullDown();
			}

			@Override
			public void onLoadMore() {
				pullUp();
			}

		});
		pullUp();
		pullDown();
		

	}
	private void pullDown() {
		pageNum = 1;
		pageSize=10;
		MyJsonRequest.Builder<ArrayList<ProducenterDeleBean.DeleteBeanProducer>> builder = new MyJsonRequest.Builder<ArrayList<ProducenterDeleBean.DeleteBeanProducer>>()
				.param("post_category",id)
				.apiVer("100")
				.param("curpage", "" + pageNum) 
				.param("pagesize",pageSize)
				.typeKey("BY_ProductCenter_videoexplain")
				.requestListener(new XRequestListener<ArrayList<ProducenterDeleBean.DeleteBeanProducer>>() {

					@Override
					public void onResponse(final ArrayList<ProducenterDeleBean.DeleteBeanProducer> bean) {
	
						adapter=new ShiPinAdapter(bean);
						xlistview.setAdapter(adapter);
						pageNum++;
						xlistview.stopRefresh();
						dataList.clear();
						xlistview.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								
								Intent intent = new Intent();
								intent.setAction("android.intent.action.VIEW");
								Uri content_url = Uri
										.parse(NetPublicConstant.URL+"/Webview/HomeView/viewvideo/id/"+bean.get(position-1).getId());
								intent.setData(content_url);
								startActivity(intent);
							}
						});
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopRefresh();
					}
				});

		MyJsonRequest<ArrayList<ProducenterDeleBean.DeleteBeanProducer>> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	
	
	private void pullUp() {
		pageNum = 1;
		pageSize=10;
		MyJsonRequest.Builder<ArrayList<ProducenterDeleBean.DeleteBeanProducer>> builder = new MyJsonRequest.Builder<ArrayList<ProducenterDeleBean.DeleteBeanProducer>>()
				.param("post_category",id)
				.apiVer("100")
				.param("curpage", "" + pageNum) 
				.param("pagesize",pageSize)
				.typeKey("BY_ProductCenter_videoexplain")
				.requestListener(new XRequestListener<ArrayList<ProducenterDeleBean.DeleteBeanProducer>>() {

					@Override
					public void onResponse(ArrayList<ProducenterDeleBean.DeleteBeanProducer> bean) {
	
						adapter=new ProductDetailAdapter(bean);
						xlistview.setAdapter(adapter);
						pageNum++;
						xlistview.stopRefresh();
						dataList.clear();
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopRefresh();
					//	onPullDownError(adapter, xlistview, exception, code, msg);
					}
				});

		MyJsonRequest<ArrayList<ProducenterDeleBean.DeleteBeanProducer>> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	
 
}
